package com.googlecode.mgwt.examples.showcase.client;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.googlecode.mgwt.dom.client.event.tap.HasTapHandlers;
import com.googlecode.mgwt.examples.showcase.client.activities.elements.WaterCountView;
import com.googlecode.mgwt.examples.showcase.client.i18n.Labels;
import com.googlecode.mgwt.examples.showcase.shared.ServiceCallDTO;
import com.googlecode.mgwt.ui.client.MGWTStyle;
import com.googlecode.mgwt.ui.client.dialog.ConfirmDialog;
import com.googlecode.mgwt.ui.client.dialog.ConfirmDialog.ConfirmCallback;
import com.googlecode.mgwt.ui.client.widget.Button;
import com.googlecode.mgwt.ui.client.widget.ProgressIndicator;
import com.googlecode.mgwt.ui.client.widget.RoundPanel;

public class WaterCountViewImpl extends DetailViewGwtImpl implements WaterCountView {

	private RoundPanel detailsPanel;
	private Button closeCallButton;
	private ProgressIndicator progressIndicator;
	private FlowPanel container ;
	private HTML html;
	
	public WaterCountViewImpl() {
		progressIndicator = new ProgressIndicator();
		progressIndicator.setVisible(false);
		scrollPanel.setScrollingEnabledX(false);
		detailsPanel = new RoundPanel();
		detailsPanel.getElement().setAttribute("style", "text-align:center");
		detailsPanel.setHeight("200px");
		closeCallButton = new Button(AppController.getClientFactory().getUserProfile().getCurrentUser().getLabels().closeCall()); 
		 container = new FlowPanel();
		container.getElement().getStyle().setMarginTop(20, Unit.PX);
		scrollPanel.setWidget(container);
		container.add(detailsPanel);
		container.add(closeCallButton);
		container.add(progressIndicator);
		 html = new HTML();
		 detailsPanel.add(html);
		
	}

	@Override
	public void setReportDetailes(ServiceCallDTO dto) {
		ServiceCallRenderer renderer = AppController.getClientFactory().getUserProfile().getCurrentUser().getServiceCallRenderer();
		Labels labels = AppController.getClientFactory().getUserProfile().getCurrentUser().getLabels();
		html.setHTML("<p style='text-align: center; position: relative; top: 14px; font-size: 14px'>"+
				labels.dateReceived()+" "+
				DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT).format(dto.getDate())+" "+
				labels.timeReceived()+" "+DateTimeFormat.getFormat(PredefinedFormat.TIME_SHORT).format(dto.getDate())+"<br>"+
				renderer.getDisplayName(dto)+"<br>"+
				renderOptional(dto)+"<p>");
		
		
		
		

		
	}

	private String renderOptional(ServiceCallDTO dto) {
		Labels labels = AppController.getClientFactory().getUserProfile().getCurrentUser().getLabels();
		StringBuilder builder = new StringBuilder();
		if(dto.getDescription()!=null && !dto.getDescription().isEmpty()){
			builder.append(dto.getDescription()+"<br>");
		}
		if(dto.getReporter()!=null && !dto.getReporter().isEmpty()){
			builder.append(labels.reportBy()+" "+ dto.getReporter());
		}
		if(dto.getReporterContact()!=null && !dto.getReporterContact().isEmpty()){
			builder.append(","+dto.getReporterContact());
		}
		return builder.toString();
	}

	@Override
	public HasTapHandlers getCloseCallButton() {
		return closeCallButton;
	}
	
	

	@Override
	public void setBussy(boolean b) {
		closeCallButton.setVisible( ! b);
		progressIndicator.setVisible(b);
			
		}

	@Override
	public void confirm(String displayString, String closeCallConfirmation,ConfirmCallback confirmCallback) {
		ConfirmDialog confirmDialog = new ConfirmDialog(MGWTStyle.getTheme().getMGWTClientBundle().getDialogCss(),
				displayString, closeCallConfirmation, confirmCallback,AppController.getClientFactory().getUserProfile().getCurrentUser().getLabels().ok(),
				AppController.getClientFactory().getUserProfile().getCurrentUser().getLabels().cancel());
		confirmDialog.show();
		
	}
}
